
************************************************************************;

options ls=100 nofmterr ;

libname raw '/data/Medicare_P01_2009/data/HEDIS/rawdata';
libname new '/data/Medicare_P01_2009/data/HEDIS/sasdata' ;
libname plan '/data/Medicare_P01_2009/data/HEDIS/plandata';


data a0; set new.hedis2005;

   *************************************************;
   ** Demographic Variables and Sample Selection  **;
   *************************************************;
 
     if denom_flag=1;
     if state_code=' ' or state_code='00' or state_code>"53" or state_code in ("40","48") then delete;

     if age>=64; /** Age 65 at end of year **/

     if plan_type='HMO/HMOPOS' ;

     if age>65 and totmonth<12 then delete;

     death_dt=bene_death_dt;
     if (bene_valid_death_dt_sw='V' or death_dt>0) then death1=1; else death1=0;

     deathyear=year(death_dt);
     deathmon =month(death_dt);

     if death1=1 and
       (deathyear=2005 or
       (deathyear=2006 and (deathmon>0 and deathmon<3)))
     then delete ;


    ******************************************;
    ** Recoding and Cleaning All Measures   **;
    ******************************************;

     ***** Quality Rates *****;

     array rate bcs_d bcs_n cmc_d cmc_n1 cdc_d1 cdc_n1 cdc_d3 cdc_n3 cdc_d4 cdc_n4 gso_d gso_n;
     do over rate;
        if rate=9 then rate=0;
     end;

             *-- cleaning BCS --*;

     if sex=1 or age>69 then bcs_d=.;
     if bcs_d=. or bcs_d=0 then bcs_n=.; 

             *-- cleaning CMC --*;

     if cmc_d=0 then cmc_n1=.; 
 
             *-- cleaning CDC --*;

     if age>75 then do; cdc_d1=.;  cdc_d2=.; cdc_d3=.; end;
     if cdc_d1=. or cdc_d1=0 then cdc_n1=.; 
     if cdc_d3=. or cdc_d3=0 then cdc_n3=.; 
     if cdc_d4=. or cdc_d4=0 then cdc_n4=.; 

             *-- cleaning GSO --*;

     if gso_d=0 then gso_n=.; 


     *****  Frequent Selected Procedures  *****;

     array uses fspcabgn fspptcan fsp_cc_n fsp_ce_n fsp_rffn fsp_thrn fsp_tkrn
                fsp_xlin fsp_oc_n fsp_clcn fsp_ah_n fsp_vh_n fsp_p_n fsp_masn fsp_lumn
                acd_n_t  acd_s_n  acd_m_n acdy_n_t acdy_s_n acdy_m_n
                ac_ov_n  ac_erv_n ac_asp_n ac_ors_n ;
     do over uses;
        if uses=999 then uses=0;
     end;

            *-- Gender specific Procedures --*;

     if sex=1 then do; fsp_ah_n=.; fsp_vh_n=.; fsp_masn=.; fsp_lumn=.; end;  /** female only **/
     if sex=2 then do; fsp_p_n=.;              end;  /** male only **/


     ***** CAP measures  John and Bruce email 6-4-2010 *****;

     if fspcabgn>3 then fspcabgn=3;   
     if fspptcan>4 then fspptcan=4;   
     if fsp_cc_n>4 then fsp_cc_n=4;   
     
     array fps2 fsp_ce_n fsp_rffn fsp_thrn fsp_tkrn fsp_xlin fsp_oc_n fsp_clcn fsp_ah_n fsp_vh_n fsp_p_n fsp_masn fsp_lumn;
     do over fps2; 
          if fps2>2 then fps2=2;  
     end;
       
     if acd_n_t > 12 then acd_n_t = 12;   * Total discharges set max 1 per month * ;    
     if acd_s_n > 12 then acd_s_n = 12;   * Surgery discharges *;
     if acd_m_n > 12 then acd_m_n = 12;   * Medical discharges *;

     if acdy_n_t > 365 then acdy_n_t = 365;  * Maximum possible in 12 months *;
     if acdy_s_n > 365 then acdy_s_n = 365;
     if acdy_m_n > 365 then acdy_m_n = 365;

     if ac_ov_n  > 50 then ac_ov_n  = 50; * Tot Out Patient visit set max 1 per week *;
     if ac_erv_n > 12 then ac_erv_n = 12; * Set max 1 per month *;
     if ac_asp_n > 12 then ac_asp_n = 12; 
     if ac_ors_n > 12 then ac_ors_n = 12;
 

     ********************************************************************;
     ** Rename All Variables and Will Keep Same Var Name for All Years **;
     ********************************************************************;

     n_cabg = fspcabgn ;  label n_cabg='CABG';
     n_ptca = fspptcan ;  label n_ptca='PTCA';
     n_cc   = fsp_cc_n ;  label n_cc  ='CardiacCathetherization';
     n_ce   = fsp_ce_n ;  label n_ce  ='CardiacEnarterectomy';
     n_rff  = fsp_rffn ;  label n_rff ='ReductionFemurFracture';
     n_thr  = fsp_thrn ;  label n_thr ='TotalHipReplacement';
     n_tkr  = fsp_tkrn ;  label n_tkr ='TotalKneereplacement';
     n_xli  = fsp_xlin ;  label n_xli ='ExcisionLargeIntest';
     n_oc   = fsp_oc_n ;  label n_oc  ='OpenCholecystectomy';
     n_clc  = fsp_clcn ;  label n_clc ='ClosedCholecstectomy';
     n_ah   = fsp_ah_n ;  label n_ah  ='AbdominalHysterectomy';
     n_vh   = fsp_vh_n ;  label n_vh  ='VaginalHysterectomy';
     n_p    = fsp_p_n ;   label n_p   ='Prostatectomy';
     n_mast = fsp_masn ;  label n_mast='Masterctomy' ;
     n_lump = fsp_lumn ;  label n_lump='Lumpectomy' ;


     iptotdis   = acd_n_t ;  label iptotdis ='InpatTotalDischarges';
     ipsurdis   = acd_s_n ;  label ipsurdis ='InpatSurgeryDischarges';
     ipmeddis   = acd_m_n ;  label ipmeddis ='InpatMedicineDischarges';

     iptotdays  = acdy_n_t ; label iptotdays ='InpatTotalDays';
     ipsurdays  = acdy_s_n ; label ipsurdays ='InpatSurgeryDays';
     ipmeddays  = acdy_m_n ; label ipmeddays ='InpatMedicineDays';

     amb_opv =  ac_ov_n ;   label amb_opv ='OutPatientVisits';
     amb_erv =  ac_erv_n ;  label amb_erv ='EmergencyRoomVisits';
     amb_asp =  ac_asp_n ;  label amb_asp ='AbmSugeryProcedures';
     amb_ors =  ac_ors_n ; label amb_ors ='ObservationRoomStays';


data a; set a0;

     array items     bcs_d bcs_n cmc_d cmc_n1 cdc_d1 cdc_d3 cdc_d4 cdc_n1 cdc_n3 cdc_n4 gso_d gso_n
                     n_cabg n_ptca n_cc n_ce n_rff n_thr n_tkr n_xli n_oc n_clc n_ah n_vh n_p n_mast n_lump
                     iptotdis ipsurdis ipmeddis iptotdays ipsurdays ipmeddays amb_opv amb_erv amb_asp amb_ors;
     do over items; if items>0 then items=1; end;

     %let all_vars= bcs_n    cmc_n1   cdc_n1   cdc_n3    cdc_n4   gso_n
                    n_cabg   n_ptca   n_cc     n_ce      n_rff    n_thr
                    n_tkr    n_xli    n_oc     n_clc     n_ah     n_vh n_p n_mast n_lump
                    iptotdis ipsurdis ipmeddis iptotdays ipsurdays ipmeddays
                    amb_opv  amb_erv  amb_asp  amb_ors ;

     proc means data=a mean n nmiss nway;
               var &all_vars;
               output out=cr0
                      mean=non0_bcs    non0_cmc   non0_cdc1   non0_cdc3   non0_cdc4   non0_gso
                           non0_cabg   non0_ptca  non0_cc     non0_ce     non0_rff    non0_thr
                           non0_tkr    non0_xli   non0_oc     non0_clc    non0_ah     non0_vh    non0_p  non0_mast non0_lump
                           non0_tdis   non0_sdis  non0_mdis   non0_tday   non0_sday   non0_mday
                           non0_opv    non0_erv   non0_asp    non0_ors 
                      n=   totn_bcs    totn_cmc   totn_cdc1   totn_cdc3   totn_cdc4   totn_gso
                           totn_cabg   totn_ptca  totn_cc     totn_ce     totn_rff    totn_thr
                           totn_tkr    totn_xli   totn_oc     totn_clc    totn_ah     totn_vh    totn_p  totn_mast totn_lump
                           totn_tdis   totn_sdis  totn_mdis   totn_tday   totn_sday   totn_mday
                           totn_opv    totn_erv   totn_asp    totn_ors ;


     proc means data=a mean noprint nway;
               class org_id;
               var &all_vars;
               output out=cr1
                      mean=hnon0_bcs    hnon0_cmc   hnon0_cdc1   hnon0_cdc3   hnon0_cdc4   hnon0_gso
                           hnon0_cabg   hnon0_ptca  hnon0_cc     hnon0_ce     hnon0_rff    hnon0_thr
                           hnon0_tkr    hnon0_xli   hnon0_oc     hnon0_clc    hnon0_ah     hnon0_vh
                           hnon0_p      hnon0_mast  hnon0_lump
                           hnon0_tdis   hnon0_sdis  hnon0_mdis   hnon0_tday   hnon0_sday   hnon0_mday
                           hnon0_opv    hnon0_erv   hnon0_asp    hnon0_ors
                      n=   hn_bcs    hn_cmc   hn_cdc1   hn_cdc3   hn_cdc4   hn_gso
                           hn_cabg   hn_ptca  hn_cc     hn_ce     hn_rff    hn_thr
                           hn_tkr    hn_xli   hn_oc     hn_clc    hn_ah     hn_vh     hn_p  hn_mast hn_lump
                           hn_tdis   hn_sdis  hn_mdis   hn_tday   hn_sday   hn_mday
                           hn_opv    hn_erv   hn_asp    hn_ors;


 data bcr0; set cr0;
      cons=1;
      proc sort; by cons;

 data bcr1; set cr1;
      cons=1;
      proc sort; by cons;

data new.cr2005(drop=_FREQ_ _TYPE_ cons); merge bcr0 bcr1; by cons;
     proc print data=new.cr2005(obs=5);

endsas;

